//
//  YMNetworkTools.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/22.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit
import AFNetworking

enum YMNetworkToolsType:String{
    
    case GET = "GET"
    case POST = "POST"
}

class YMNetworkTools: AFHTTPSessionManager {
    
    static let sharedTools:YMNetworkTools = {
       let tools = YMNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()

    
    func request(type:YMNetworkToolsType, urlString:String ,parameters:AnyObject? ,success:(response:AnyObject?) ->() ,error:(error:NSError) ->()) {
        
        
        if type == .GET {
            
            GET(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response:response)
            }) { (_, err) in
                error(error:err)
            }
        }else{
            POST(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response:response)
 
            }) { (_, erro) in
                
                error(error:erro)
                print(erro,"小的错了")

            }
        }
    }
}


//        MARK: - 请求OAuth界面数据

extension YMNetworkTools{
    
    //请求userAccount
    
    func oauthLoadAccount(code:String ,success:(response:AnyObject?) ->() ,error:(error:NSError) ->()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = [
            "client_id":APPKEY,
            "client_secret":APPSECRET,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":APPREDIRECT_URI
        ]
        
        request(.POST, urlString: urlString, parameters: params, success: success, error: error)
        
    }
    
//=======================================
    
    //请求用户信息数据
    
    func oauthLoadUserInfo(userAccount:YMUserAccount ,success:(response:AnyObject?) ->() ,error:(error:NSError) ->()) {
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let params = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        
        request(.GET, urlString: urlString, parameters: params, success: success, error: error)
        
    }
    
}



//==========================================

//        MARK: - 请求微博首页原创微博数据

extension YMNetworkTools{
    
   
    func loadHomeData(sinceId: Int64 ,maxId: Int64 ,success: (response:AnyObject?) ->() ,failure:(error:NSError) ->()) {
        
    let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = ["access_token":YMUserAccountViewModel.sharedTools.accessToken!,
                      "since_id":"\(sinceId)",
                      "max_id":"\(maxId)"]

        request(.GET, urlString: urlString, parameters: params, success: success, error: failure)
        
    }

    
    
}

//MARK: 发布微博

extension YMNetworkTools{
    
    //文字微博
    func compiseUpdate(status: String, success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
        
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token":YMUserAccountViewModel.sharedTools.accessToken!,
            "status":status
        ]
        
        request(.POST, urlString: urlString, parameters: params, success: success, error: failure)
    }
    
    //图片微博
    
    func composeUpload(status: String, imageList: [UIImage], success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        // 请求参数
        let params = [
            "access_token":YMUserAccountViewModel.sharedTools.accessToken!,
            "status":status
        ]
        

        
        POST(urlString, parameters: params, constructingBodyWithBlock: { (formdata) in
            
            for i in 0..<imageList.count{
                let data = UIImagePNGRepresentation(imageList[i])!
                formdata.appendPartWithFileData(data, name: "pic", fileName: "oooo", mimeType: "application/octet-stream")
            }
            
            
            
            
            
            }, success: { (_, response) in
              //成功
                success(response: response)
            }) { (_, error) in
                //失败
                failure(error: error)
        }
        
    }
    
    
}










